package jjn.round1;

import java.util.Arrays;

/**
 * @author Jjn
 * @since 2022/9/17 23:49
 */
public class LeetCode1624_LargestSubstringBetweenTwoEqualCharacters {
    public int maxLengthBetweenEqualCharacters(String s) {
        int[] firstIndex = new int[26];
        Arrays.fill(firstIndex, -1);
        int maxLength = -1;
        for (int i = 0; i < s.length(); i++) {
            int index = s.charAt(i) - 'a';
            // 第一次出现, 下标赋值给对应位置
            if (firstIndex[index] < 0) {
                firstIndex[index] = i;
            } else {
                // 后续出现的话, 算一下差值
                maxLength = Math.max(maxLength, i - firstIndex[index] - 1);
            }
        }
        return maxLength;
    }
}
